﻿using DemoQLKhoSX.Library;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DemoQLKhoSX.DatabaseAccessLayer
{
    public class KhachHangDAO
    {
        private DBConnection conn;

        public KhachHangDAO()
        {
            conn = new DBConnection();
        }

        public DataTable searchByID(string id)
        {
            string query = string.Format("select * from TBL_khachHang where IDKhachHang = @idkhach or @idkhach=''");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@idkhach", SqlDbType.VarChar);
            sqlParameters[0].Value = Convert.ToString(id);
            return conn.executeSelectQuery(query, sqlParameters);
        }
        //public void LoadData()
        //{
        //    string query = string.Format("select * from TBL_khachHang");
        //    SqlParameter[] sqlParameters = new SqlParameter[0];
        //    conn.executeSelectQuery(query,sqlParameters);
        //}

        public bool ThemKhachHang(string id,string tenkh,string sodt,string sotk,string diachi)
        {
            string query = string.Format("insert into TBL_khachHang(IDKhachHang,TenKH,SoDT,SoTK,DiaChi) values(@idkhachhang,@tenkh,@sodt,@sotk,@diachi)");
            SqlParameter[] sqlParameters = new SqlParameter[5];
            sqlParameters[0] = new SqlParameter("@idkhachhang", SqlDbType.Char);
            sqlParameters[0].Value = Convert.ToString(id);
            sqlParameters[1] = new SqlParameter("@tenkh", SqlDbType.VarChar);
            sqlParameters[1].Value = Convert.ToString(tenkh);
            sqlParameters[2] = new SqlParameter("@sodt", SqlDbType.VarChar);
            sqlParameters[2].Value = Convert.ToString(sodt);
            sqlParameters[3] = new SqlParameter("@sotk", SqlDbType.VarChar);
            sqlParameters[3].Value = Convert.ToString(sotk);
            sqlParameters[4] = new SqlParameter("@diachi", SqlDbType.VarChar);
            sqlParameters[4].Value = Convert.ToString(diachi);
            return conn.executeInsertQuery(query, sqlParameters);
        }

        public void SuaKhachHang(string id,string tenkh,string sodt,string sotk,string diachi)
        {
            string query = string.Format("update TBL_khachHang set TenKH=@tenkh,SoDT=@sodt,SoTK=@sotk,DiaChi=@diachi where IDKhachHang=@idkhachhang");
            SqlParameter[] sqlParameters = new SqlParameter[5];
            sqlParameters[0] = new SqlParameter("@idkhachhang", SqlDbType.VarChar);
            sqlParameters[0].Value = Convert.ToString(id);
            sqlParameters[1] = new SqlParameter("@tenkh", SqlDbType.VarChar);
            sqlParameters[1].Value = Convert.ToString(tenkh);
            sqlParameters[2] = new SqlParameter("@sodt", SqlDbType.VarChar);
            sqlParameters[2].Value = Convert.ToString(sodt);
            sqlParameters[3] = new SqlParameter("@sotk", SqlDbType.VarChar);
            sqlParameters[3].Value = Convert.ToString(sotk);
            sqlParameters[4] = new SqlParameter("@diachi", SqlDbType.VarChar);
            sqlParameters[4].Value = Convert.ToString(diachi);
            conn.executeUpdateQuery(query, sqlParameters);
        }

        public void XoaKhachHang(string id)
        {
            string query = string.Format("delete from TBL_khachHang where IDKhachHang=@idkhachhang");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@idkhachhang", SqlDbType.VarChar);
            sqlParameters[0].Value = Convert.ToString(id);
            conn.executeDeleteQuery(query, sqlParameters);
        }
    }
}
